package com.example.dllo.moondog.information.newsss;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseFragment;
import com.example.dllo.moondog.information.newsss.loadpicture.Advertisements;
import com.example.dllo.moondog.information.newsss.loadpicture.LoadBeans;
import com.example.dllo.moondog.tools.GsonRequest;
import com.example.dllo.moondog.tools.ReadStringRequestUtf;
import com.example.dllo.moondog.tools.VolleySingleton;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by dllo on 16/3/2.
 */
public class NewFragment extends BaseFragment {
    private Context context;
    private ListView listView;
    private LinearLayout llAdvertiseBoard;
    private LayoutInflater inflater;
    private String loadUrl = "http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_information);
        LinearLayout header = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.fragment_boy_book_head, null);
        listView.addHeaderView(header);
        initViews();

    }

    //添加轮播图图片
    private void initViews() {
        llAdvertiseBoard = (LinearLayout) getActivity().findViewById(R.id.llAdvertiseBoard);
        // 添加图片的Url地址

        ReadStringRequestUtf requestUtf = new ReadStringRequestUtf(loadUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                LoadBeans loadBeans = gson.fromJson(response, LoadBeans.class);

                JSONArray advertiseArray = new JSONArray();
                try {
                    JSONObject head_img0 = new JSONObject();
                    head_img0.put("head_img", loadBeans.getList().get(0).getImage_url_big());
                    JSONObject head_img1 = new JSONObject();
                    head_img1.put("head_img", loadBeans.getList().get(1).getImage_url_big());
                    JSONObject head_img2 = new JSONObject();
                    head_img2.put("head_img", loadBeans.getList().get(2).getImage_url_big());
                    JSONObject head_img3 = new JSONObject();
                    head_img3.put("head_img", loadBeans.getList().get(3).getImage_url_big());
                    advertiseArray.put(head_img0);
                    advertiseArray.put(head_img1);
                    advertiseArray.put(head_img2);
                    advertiseArray.put(head_img3);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                llAdvertiseBoard.addView(new Advertisements(getActivity(), true, inflater, 3000).initView(advertiseArray));

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(requestUtf);

    }

}
